package com.loongtech.bi.entity.system;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.loongtech.core.util.Utils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Entity
@Table(name = "bi_server")
@JsonIgnoreProperties({"comparatorId"})
public class EntityBiServer implements Serializable {
    private static final long serialVersionUID = 3L;
    @Id
    private String id; // 主键
    // 游戏名称
    @Transient
    private String gameName;
    // 游戏id
    @Column(columnDefinition = "int(11) default 0")
    private Integer gameId;
    @Column(columnDefinition = "varchar(255) NOT NULL default ''")
    private String name;
    @Column(columnDefinition = "varchar(255) NOT NULL default ''")
    private String serverType;  // 服务器分组
    @Column(columnDefinition = "varchar(255) default ''")
    private String description;
    @Column(columnDefinition = "varchar(64) NOT NULL default '1'")
    private String channelIds;
    @Transient
    private List<String> channelIdList = new ArrayList<>();
    @Transient
    private List<String> channelNameList = new ArrayList<>();

    private Integer capacity = 8000;  // 服务器容量

    private boolean used = true;  // 是否可用

    private long updateAt;        // 最后更新时间

    @PrePersist
    @PreUpdate
    public void preUpdate4UpdateAt() {
        this.setUpdateAt(System.currentTimeMillis());
    }

    @Transient
    public final Comparator<EntityBiServer> comparatorId = new Comparator<EntityBiServer>() {
        public int compare(EntityBiServer s1, EntityBiServer s2) {
            return s1.getId().compareTo(s2.getId());
        }
    };

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getGameName() {
        return gameName;
    }

    public void setGameName(String gameName) {
        this.gameName = gameName;
    }

    public Integer getGameId() {
        return gameId;
    }

    public void setGameId(Integer gameId) {
        this.gameId = gameId;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + gameName.hashCode();
        result = 31 * result + gameId.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + serverType.hashCode();
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + channelIds.hashCode();
        result = 31 * result + channelIdList.hashCode();
        result = 31 * result + capacity;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        EntityBiServer other = (EntityBiServer) obj;
        return id == other.id;
    }

    @Override
    public String toString() {
        return "EntityBiServer{" +
                "id='" + id + '\'' +
                ", gameName='" + gameName + '\'' +
                ", gameId=" + gameId +
                ", name='" + name + '\'' +
                ", serverType='" + serverType + '\'' +
                ", description='" + description + '\'' +
                ", channelIds='" + channelIds + '\'' +
                ", channelIdList=" + channelIdList +
                ", capacity=" + capacity +
                '}';
    }

    public long getUpdateAt() {
        return updateAt;
    }

    public void setUpdateAt(long updateAt) {
        this.updateAt = updateAt;
    }

    public String getChannelIds() {
        return channelIds;
    }

    public void setChannelIds(String channelIds) {
        this.channelIds = channelIds;
        if (channelIds != null) {
            for (String c : channelIds.split(",")) {
                if (!Utils.isEmptyStr(c)) {
                    channelIdList.add(c);
                }
            }
        }
    }

    public List<String> getChannelIdList() {
        if (channelIdList.size() <= 0 && channelIds != null) {
            for (String c : channelIds.split(",")) {
                if (!Utils.isEmptyStr(c)) {
                    channelIdList.add(c);
                }
            }
        }

        return channelIdList;
    }

    public List<String> getChannelNameList() {
        return channelNameList;
    }

    public void setChannelNameList(List<String> channelNameList) {
        this.channelNameList = channelNameList;
    }

    public void setChannelIdList(List<String> channelIdList) {
        this.channelIdList = channelIdList;
    }

    public Integer getCapacity() {
        return capacity;
    }

    public void setCapacity(Integer capacity) {
        this.capacity = capacity;
    }

    public boolean isUsed() {
        return used;
    }

    public void setUsed(boolean used) {
        this.used = used;
    }

    public static class K {
        public static final String id = "id";
        public static final String name = "name";
        public static final String description = "description";

        // id, name, description,
        public static java.util.List<String> toList() {
            java.util.List<String> list = new java.util.ArrayList<>();
            list.add(id);
            list.add(name);
            list.add(description);
            return list;
        }
    }

    public String getServerType() {
        return serverType;
    }

    public void setServerType(String serverType) {
        this.serverType = serverType;
    }
}